﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Iris.Engine;
using System.Windows.Threading;
using System.Threading;

namespace Iris.UI
{
    /// <summary>
    /// Logique d'interaction pour JobWindow.xaml
    /// </summary>
    public partial class JobWindow : Window
    {
        private List<ImageSort> images;

        public static JobWindow Create(List<ImageSort> images ) {
            JobWindow job = new JobWindow();
            job.images = images;
            return job;
        }

        public JobWindow()
        {
            InitializeComponent();   
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            

            SortCore.singleton.UpdateFiveStarEvent += on_UpdateFiveStarEvent;
            SortCore.singleton.UpdateFiveStarFailedEvent += on_UpdateFiveStarFailedEvent;
            SortCore.singleton.EndUpdateFiveStarEvent += on_EndUpdateFiveStarEvent;

            SortCore.singleton.BeginUpdateFiveStar(images);
        }

        private void expander1_Expanded(object sender, RoutedEventArgs e)
        {
            this.Height = 550;
        }

        private void expander1_Collapsed(object sender, RoutedEventArgs e)
        {
            this.Height = 220;
        }

        
        private void on_EndUpdateFiveStarEvent() {
            textBlock.Dispatcher.Invoke(new Action(delegate()
            {
                textBlock.Text += "Processus terminée.";
                
                new NotificationWindow("Tri terminé : Sauvez les notes.").Show();
                Thread.Sleep(2000);
                this.Close();

            }), DispatcherPriority.Render);

            
            }

        private void on_UpdateFiveStarFailedEvent(string message)
        {
            Dispatcher.Invoke(new Action(delegate()
            {
                textBlock.Text += String.Format("erreur : {0}\r\n", message);
            }), DispatcherPriority.Render);   
        }

        private void on_UpdateFiveStarEvent(string message, int index, int Total)
        {
            progressBar.Dispatcher.Invoke(new Action(delegate()
            {
                progressBar.Value = (index / Total * 100);
            }), DispatcherPriority.Render);

            textBlock.Dispatcher.Invoke(new Action(delegate()
            {
                textBlock.Text += String.Format("message : {0}\r\n", message);

            }), DispatcherPriority.Render);
        }
    }
}
